<template>
  <!-- 注册页面的整体盒子 -->
  <div class="register-container">
    <!-- 注册的盒子 -->
    <div class="register-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form ref="formRef" :model="regForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="regForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="regForm.password" placeholder="请输入密码" show-password prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input v-model="regForm.repassword" placeholder="请确认密码" show-password prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReg">立即注册</el-button>
          <el-link type="info" @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 导入api
import { registerAPI } from '../../api/register.js'

export default {
  name: 'Register',
  data() {
    // 密码确认
    const validateRepass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.regForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 名字和接口名字一致,方便上传数据
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      // 表单验证规则 名字和数据名字保持一致
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-z\d]{2,10}$/i, message: '用户名必须是2-10的字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          // 使用密码确认方法
          { validator: validateRepass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 兜底校验
    handleReg() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        // 校验通过
        const { data: res } = await registerAPI(this.regForm)
        if (res.code === 0) {
          // 提示
          this.$message.success(res.message)
          // 跳转到登录页
          this.$router.push('/login')
        } else {
          // 失败提示
          this.$message.warning(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  background: url('~@/assets/images/bg.jpg') center;
  background-size: cover;
  height: 100%;

  .register-box {
    width: 400px;
    height: 405px;
    background-color: #fff;
    border-radius: 3px;
    padding: 35px 30px;
    box-sizing: border-box;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url('~@/assets/images/login_title.png') center no-repeat;
    }
  }
  .el-button {
    width: 100%;
  }
}
</style>
